<?php

namespace app\api\controller;

use app\admin\model\{Orders, Sales, SalesDetail, SystemUploadfile, Users};
use app\ApiBaseController;
use Qcloud\Sms\SmsSingleSender;
use think\facade\Cache;

class User extends ApiBaseController
{
    /**
     * 设置用户头像
     */
    public function setHeadImg()
    {
        $img_id = $this->request->param('img_id/d');
        $img = SystemUploadfile::find($img_id);
        if (empty($img)){
            return result(1,'图片id不存在');
        }
        try {
            Users::update(['head_img_id' => $img_id], ['uid' => $this->uid]);
        }catch (\Exception $e){
            return result(1,'设置失败：'.$e->getMessage());
        }
        return result(0,'设置成功');
    }

    /**
     * 获取用户信息
     */
    public function getUserInfo()
    {
        $user = $this->user;
        $where = [['uid','=',$this->uid], ['state', '>', 0]];
        $user['all_total_amount'] = Orders::where($where)
            ->sum('total_amount');
        $user['all_pay_amount'] = Orders::where($where)
            ->sum('pay_amount');
        $user['today_total_amount'] = Orders::where($where)
            ->whereDay('create_time')
            ->sum('total_amount');
        $user['today_pay_amount'] = Orders::where($where)
            ->whereDay('create_time')
            ->sum('pay_amount');
        $user['yesterday_total_amount'] = Orders::where($where)
            ->whereDay('create_time','yesterday')
            ->sum('total_amount');
        $user['yesterday_pay_amount'] = Orders::where($where)
            ->whereDay('create_time','yesterday')
            ->sum('pay_amount');
        $user['head_img'] = SystemUploadfile::where('id', $user['head_img_id'])->value('url');
		$user['ranking'] = Cache::get('index_month_ranking');
        return result(0, '', $user);
    }

    /**
     * 发送验证码
     */
    public function sendSms()
    {
        if (Cache::get('sms_'.$this->uid)){
            return result(1,'距离上次发送不足'.config('sms.interval').'秒,请等待');
        }
        $code = mt_rand(100000,999999);
        Cache::set('sms_'.$this->uid, $code, config('sms.interval'));

        Cache::set('sms_expire_'.$this->uid, config('sms.expire'));

        return result(0,'', ['code'=>$code]);
        try {
            $ssender = new SmsSingleSender(config('sms.appid'), config('sms.appkey'));
            $result = $ssender->sendWithParam("86", $this->user->phone, '8888', [$code], config('sms.sms_sign'), "", "");
            $rsp = json_decode($result);
        } catch(\Exception $e) {
            return result(1, '发送失败：'.$e->getMessage());
        }
        if($rsp->result == 0){
            Cache::set('sms_expire_'.$this->uid, config('sms.expire'));
            return result(0,'发送成功');
        }
        return result(1,'发送失败');
    }

    /**
     * 修改密码
     */
    public function setPassword()
    {
        $post = $this->request->post();
        $rule = [
            'password|密码'        =>  'require',
            'repassword|重复密码'  =>  'require|confirm:password',
            'code|验证码'          =>  'require|number|length:6',
        ];
        $this->apiValidate($post, $rule);
        if ($true_code = Cache::get('sms_'.$this->uid)){
            if ($true_code == $post['code']){
                try {
                    $user = Users::find($this->uid);
                    $user->password = password($post['password']);
                    $save = $user->save();
                }catch (\Exception $e){
                    return result(1,'修改失败：'.$e->getMessage());
                }
                if (!empty($save)){
                    return result(0,'修改成功');
                }
                return result(1,'修改失败');
            }
            return result(1,'验证码不正确');
        }
        return result(1,'验证码不存在或已过期,请重新发送');
    }
}